//
//  MovieListViewController.m
//  SouhuTV
//
//  Created by 肖野 on 15/6/10.
//  Copyright (c) 2015年 Mike. All rights reserved.
//

#import "MovieListViewController.h"
#import "MovieDetailViewController.h"
#import "XYMainFunction.h"
#import "MJRefresh.h"


@interface MovieListViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionView* collectionView;

@property (nonatomic, retain) NSMutableArray* listArray;

@property (nonatomic, assign) NSInteger list;
@property (nonatomic, assign) NSInteger offset;
@property (nonatomic, assign) NSInteger lastList;

@end

@implementation MovieListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.listArray = [NSMutableArray array];
    self.navigationItem.title = self.title;
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((self.view.frame.size.width - 15) / 3 , self.view.frame.size.width / 3 * 1.4 + 25);
    layout.minimumInteritemSpacing = 5;
    
    _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    [self.view addSubview:_collectionView];
    
    //注册cell
    [_collectionView registerClass:[HomePageCollectionCell class] forCellWithReuseIdentifier:@"CELL"];
    [self getNetWork:30 withOffSet:0];
    [self setupRefresh];
    
}


- (void)getNetWork:(NSInteger)list withOffSet:(NSInteger)offset
{
    NSString* string = [NSString stringWithFormat:@"%@&limit=%ld&offSet=%ld", self.apiUrl, list, offset];
    [GetConnection startConnection:string parmaters:nil connetionBlock:^(id responseObject) {
        [self.listArray removeAllObjects];
        [self.listArray addObjectsFromArray:(NSArray*)responseObject];
        [self.collectionView reloadData];
        
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.listArray.count;
}


- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomePageCollectionCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];
    
    cell.baseInfo = [MovieBaseInfo movieBaseInfoWithDictionary:self.listArray[indexPath.row]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MovieDetailViewController* detailVC = [[MovieDetailViewController alloc] init];
    MovieBaseInfo* base = [MovieBaseInfo movieBaseInfoWithDictionary:self.listArray[indexPath.row]];
    [HistoryUserDefault addMovieBaseInfoInUseDefault:base];
    
    detailVC.ID = base.ID;
    [ self.navigationController pushViewController:detailVC animated:YES];
}

//- (void)setupRefresh
//{
//    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
//
//    [self.collectionView addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(headerRereshing)];
//
//    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
//
//
//    [self.collectionView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(footerReferenceSize)];
//}
//
//- (void)headerRereshing
//{
//    self.list = 30;
//    self.offset = 0;
//    [self getNetWork:self.list withOffSet:self.offset];
//}
//
//- (void)footerRereshing
//{
//
//    self.list = 30;
//    self.offset = 0;
//    [self getNetWork:self.list withOffSet:self.offset];
//    self.offset += self.list;
//
//}

- (void)setupRefresh
{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    
    [self.collectionView addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(headerRereshing)];
    
    [self.collectionView.header beginRefreshing];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self.collectionView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(footerRereshing)];
    [self.collectionView.footer beginRefreshing];
    
    
}



#pragma mark 开始进去刷新状态

-(void)headerRereshing
{
    
    self.list = 30;
    
    [self getNetWork:self.list withOffSet:self.offset];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.collectionView.header endRefreshing];
    });
    
    
    
}

- (void)footerRereshing
{
    self.list += 30;
    [self getNetWork:self.list withOffSet:self.offset];
    // 2.2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.collectionView.footer endRefreshing];
    });
    
}

//


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
